define(["jquery","jsonp"],function($,jsonp){
	return {
		init : function(res){
			//动态生成navItems
			this.navboxShow(res);
			this.inputBind();
			this.fixSearch();
		},
		
		//根据请求的ajax数据加载nav_box内容
		navboxShow : function (data){
			var html = "";
			for(var i=0; i<data.navItem.length;i++){
				if(i != 5){
					html += `<li>
						<a href="javascript:;">${data.navItem[i]}</a>
						<span class=""></span>
					</li>`
				}else{
					html += `<li>
						<a href="javascript:;">${data.navItem[i][0]}</a>
						<span class=""></span>
					</li>`
				}
				
			}
			$(".nav_box").html(html);
			//添加热点图标	
			$(".nav>.nav_box>li").eq(0).find("span").addClass("nav_hot");
			$(".nav>.nav_box>li").eq(3).find("span").addClass("nav_new");
		},
		
		
		
		//搜索模块的单击事件
		inputBind : function(){
			
			var $input = $(".nav>.search>input");
			var $searchList = $input.siblings(".searchList");
			var timer = null;
			var that = this;
//===============添加获得焦点事件
			$input.on("focus",function(){
				$(".nav>.search>em").css({"display":"none"});
				if($(this).val() != ""){
					$searchList.css("display","block");
				}
			}).on("blur",function(){
				$(".nav>.search>em").css({"display":"block"});
				$searchList.css("display","none");
				
			}).css({"outline":"none"});
			
//=============	搜索按钮点击事件
			$(".search_btn").on("click",function(){
				if($input.val() != ""){
					$(this).attr("href","list_of_goods.html?key="+$input.val());
					console.log($(this).attr("href"));
				}else{
					$(this).attr("href","list_of_goods.html?key="+"TAB4");
					console.log($(this).attr("href"));
				}
			});
			
//==============鼠标滑过列表时,去掉onblur事件
			$(".searchList").on("mouseenter",function(){
				$input.off("blur");
			}).on("mouseleave",function(){
				$input.on("blur",function(){
					$(".nav>.search>em").css({"display":"block"});
					$searchList.css("display","none");
				});
			});
			
			
//===================添加输入事件
			$input.on("keyup",function(){
				
				//异步加载数据
				var src = "https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=";
				var data = $(this).val();
				clearTimeout(timer);
				
				timer = setTimeout(function(){
					$searchList.html("");
					if(data != ""){
						$searchList.css("display","block");
						jsonp.getData(src,function(res){
							console.log("接收到服务器返回数据！");
							console.log(res);
							for(var i=0; i<res.s.length; i++){
								that.initSearchList(res.s[i]);
							}
						},data);
					}else{
						$searchList.css("display","none");
					};
				},500);
				
			});
		},
		
		//初始化搜索框页面
		initSearchList : function(content){
			var $li =$(`<li><a href="javascript:;">${content}<span class="right_item">约632个商品</span></a></li>`) ;
			$(".nav .search .searchList").append($li);
			
			//搜索按钮添加点击事件
			$li.on("click",function(){
				$(".nav>.search>input").val($(this).text());
				$(".nav .search .searchList").hide();
			});
		},
		
		//nav吸顶效果
		fixSearch : function(){
			var $nav = $(".nav");
			$(document).on("scroll",function(){
				
//				console.log();
				if($(this).scrollTop() > 24){
					$nav.addClass("fix");
					$nav.find(".logo").css({
						position:"absolute",
						left:0
					}).find("img").attr("src","img/lenovo_logo1.png");
				}else{
					$nav.removeClass("fix");
					$nav.find(".logo").css({
						position:"fixed",
						left:0
					}).find("img").attr("src","img/lenovo_logo.png");
				}
			})
			
		}
		
		
		
		
		
		
	}
})
